package cn.iocoder.yudao.module.wms.controller.admin.saleorder;

import cn.iocoder.yudao.module.wms.dal.dataobject.saleorder.SaleDetailDO;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.saleorder.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.saleorder.SaleOrderDO;

import cn.iocoder.yudao.module.wms.service.saleorder.SaleOrderService;

@Tag(name = "管理后台 - 销售订单主")
@RestController
@RequestMapping("/wms/sale-order")
@Validated
public class SaleOrderController {

    @Resource
    private SaleOrderService saleOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建销售订单主")
    @PreAuthorize("@ss.hasPermission('wms:sale-order:create')")
    public CommonResult<Integer> createSaleOrder(@Valid @RequestBody SaleOrderSaveReqVO createReqVO) {
        return success(saleOrderService.createSaleOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新销售订单主")
    @PreAuthorize("@ss.hasPermission('wms:sale-order:update')")
    public CommonResult<Boolean> updateSaleOrder(@Valid @RequestBody SaleOrderSaveReqVO updateReqVO) {
        saleOrderService.updateSaleOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除销售订单主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:sale-order:delete')")
    public CommonResult<Boolean> deleteSaleOrder(@RequestParam("id") Integer id) {
        saleOrderService.deleteSaleOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得销售订单主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:sale-order:query')")
    public CommonResult<SaleOrderRespVO> getSaleOrder(@RequestParam("id") String id) {
        try{
            SaleOrderDO saleOrder = saleOrderService.getSaleOrder(Integer.valueOf(id));
            return success(BeanUtils.toBean(saleOrder, SaleOrderRespVO.class));
        }catch (Exception e)
        {
            SaleOrderDO saleOrder = saleOrderService.findByProcessInstanceId(id);
            return success(BeanUtils.toBean(saleOrder, SaleOrderRespVO.class));
        }

    }

    @GetMapping("/page")
    @Operation(summary = "获得销售订单主分页")
    @PreAuthorize("@ss.hasPermission('wms:sale-order:query')")
    public CommonResult<PageResult<SaleOrderRespVO>> getSaleOrderPage(@Valid SaleOrderPageReqVO pageReqVO) {
        PageResult<SaleOrderDO> pageResult = saleOrderService.getSaleOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SaleOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出销售订单主 Excel")
    @PreAuthorize("@ss.hasPermission('wms:sale-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSaleOrderExcel(@Valid SaleOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SaleOrderDO> list = saleOrderService.getSaleOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "销售订单主.xls", "数据", SaleOrderRespVO.class,
                        BeanUtils.toBean(list, SaleOrderRespVO.class));
    }

    @GetMapping("/findSaleOrderOperateList")
    @Operation(summary = "获得销售订单主分页")
    @PreAuthorize("@ss.hasPermission('wms:sale-order:query')")
    public CommonResult<List<SaleOrderRespVO>> findSaleOrderOperateList() {
        List<SaleOrderDO> pageResult = saleOrderService.findSaleOrderOperateList();
        return success(BeanUtils.toBean(pageResult, SaleOrderRespVO.class));
    }
    @GetMapping("/startProcess")
    @Operation(summary = "启动流程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:sale-order:create')")
    public CommonResult<Boolean> startProcess(@RequestParam("id") Integer id) {
        saleOrderService.process(id);
        return success(true);
    }

    // ==================== 子表（销售订单子） ====================

    @GetMapping("/sale-detail/list-by-pid")
    @Operation(summary = "获得销售订单子列表")
    @Parameter(name = "orderId", description = "订单id")
    @PreAuthorize("@ss.hasPermission('wms:sale-order:query')")
    public CommonResult<List<SaleDetailDO>> getSaleDetailListByPid(@RequestParam("pid") Integer pid) {
        return success(saleOrderService.getSaleDetailListByPid(pid));
    }

    @GetMapping("/getSaleDetialById")
    @Operation(summary = "获得销售订单主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:sale-order:query')")
    public CommonResult<SaleDetialRespVO> getSaleDetialById(@RequestParam("id") Integer id) {
        return success(saleOrderService.findSaleDetialById(id));

    }
}